﻿using System;

namespace myWindowsMediaPlayer
{
    public class PlaylistItem
    {
        public String _title { get; set; }
        public String _artist { get; set; }
        public String _album { get; set; }
        public String _trackNum { get; set; }
        public String _year { get; set; }
        public String _comment { get; set; }
        public String _genre { get; set; }
        public String _path { get; set; }
        public String _length { get; set; }

        public PlaylistItem(string path)
        {
            this._path = path;
            TagLib.File audio = TagLib.File.Create(_path);
            _album = audio.Tag.Album;
            _artist = GetAllStringsFromArray(audio.Tag.AlbumArtists, ",");   // Tag.AlbumAritst is a string array 
            _genre = GetAllStringsFromArray(audio.Tag.Genres, ",");
            _title = audio.Tag.Title;
            _trackNum = audio.Tag.Track.ToString();
            _year = audio.Tag.Year.ToString();
            _length = audio.Properties.Duration.ToString();
            _length = _length.Split('.')[0];
        }

        public PlaylistItem()
        {
            // TODO: Complete member initialization
        }
        
        public void PrintTags()
        {
            Console.WriteLine("title : " + _title);
            Console.WriteLine("artist : " + _artist);
            Console.WriteLine("album : " + _album);
            Console.WriteLine("trackNum : " + _trackNum);
            Console.WriteLine("year : " + _year);
            Console.WriteLine("comment : " + _comment);
            Console.WriteLine("genre : " + _genre);
            Console.WriteLine("length : " + _length);
            Console.WriteLine("path : " + _path);
        }

        public override string ToString()
        {
            return (_artist.Length > 0 ? _artist + " - " : "") + _title + " - " + _length;
        }

        public string GetAllStringsFromArray(string[] strArray, string strDelimeter)
        {
            string strFinal = string.Empty;

            for (int i = 0; i < strArray.Length; i++)
            {
                strFinal += strArray[i];

                if (i != strArray.Length - 1)
                {
                    strFinal += strDelimeter;
                }
            }
            return strFinal;
        }
    }
}
